
#[derive(Debug)]
pub struct Person<'a> {
    name: &'a str,
    age: u8,
}

// A unit struct 空结构
pub struct Nil;

// A tuple struct 元组类型结构
pub struct Pair(i32, f32);

// A struct with two fields 双字段结构
pub struct Point {
    x: f32,
    y: f32,
}

// Structs can be reused as fields of another struct
// 结构能被其他结构重用字段，这个还不理解
#[allow(dead_code)]
pub struct Rectangle {
    p1: Point,
    p2: Point,
}
// 结构测试方法
pub fn structures_test() {
    // Create struct with field init shorthand
    let name = "Peter";
    let age = 27;
    let peter = Person { name, age };

    // Print debug struct
    println!("打印 Person 结构 peter ： {:?}", peter);


    // Instantiate a `Point`
    let point: Point = Point { x: 0.3, y: 0.4 };

    // Access the fields of the point
    println!("打印 Point 两个xy值 point coordinates: ({}, {})", point.x, point.y);

    // Make a new point by using struct update syntax to use the fields of our other one
    // ..point 简化的剩余结构赋值写法
    let new_point = Point { x: 0.1, ..point };
    // `new_point.y` will be the same as `point.y` because we used that field from `point`
    println!("打印 第二个 Point 数据： second point: ({}, {})", new_point.x, new_point.y);

    // Destructure the point using a `let` binding
    let Point { x: my_x, y: my_y } = point;

    let _rectangle = Rectangle {
        // struct instantiation is an expression too
        p1: Point { x: my_y, y: my_x },
        p2: point,
    };

    // Instantiate a unit struct
    // 初始化一个 类单元结构体 
    // 类单元结构体常常在你想要在某个类型上实现 trait 但不需要在类型内存储数据的时候发挥作用。
    let _nil = Nil;

    // Instantiate a tuple struct
    // 初始化一个元组
    let pair = Pair(1, 0.1);

    // Access the fields of a tuple struct
    // 通过元组名.0..n访问元组数据
    println!("打印 pair元组：pair contains {:?} and {:?}", pair.0, pair.1);

    // Destructure a tuple struct 用无命名元组复制命名元组，
    // 好像只是用元组绑定了两个变量名，不能真的算 命名元组
    let Pair(integer, decimal) = pair;

    println!("打印命名了的Pair结构： pair contains {:?} and {:?}", integer, decimal);
}